public class SortColors {
    public void sortColors(int[] nums) {
        int left = -1, i = 0, right = nums.length;
        while (i < right) {
            if(nums[i] == 0) {
                // swap(nums, nums[++left], nums[i++])
                swap(nums, ++left, i++);
            }else if(nums[i] == 2) {
                // swap(nums, nums[--right], nums[i])
                swap(nums, --right, i);
            }else {
                i++;
            }
        }
    }

    public void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
